<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <!-- 
            思路：后序遍历，从叶子节点向上遍历。节点的返回值是当前节点的左右子节点
            0:表示无覆盖
            1:表示当前节点有摄像头
            2:表示有覆盖
        -->
        <script>
            var minCameraCover = function (root) {
                let result = 0
                //如果根节点状态是0.没覆盖，需要添加一个
                if (postOrder(root) == 0) {
                    result++
                }
                return result
                function postOrder(root) {
                    if (!root) return 2
                    let left = postOrder(root.left)
                    let right = postOrder(root.right)
                    if (left == 2 && right == 2) {
                        return 0
                    }
                    if (left == 0 || right == 0) {
                        result++
                        return 1
                    }
                    if (left == 1 || right == 1) {
                        return 2
                    }
                    return -1
                }
            }
        </script>
    </body>
</html>
